<template>
	<view class="content">
		<!-- <permissionPopup></permissionPopup> -->
		<view class="box">
			<view class="withdrawal">
				<view class="name">
					<view class="title">
						姓名
					</view>
					<view class="input">
						<u--input placeholder="请输入真实姓名" border="none" inputAlign="right" v-model="form.name"></u--input>
					</view>
				</view>
				<view class="paytitle">
					点击上传{{form.type==1?'支付宝':'微信'}}收款码
				</view>
				<view class="uploadimg">
					<view class="uploadimg-bg">
						<view class="upload" v-if="form.image">
							<u-icon name="close-circle-fill" @click="delImage" class="del-btn" size="50rpx" bold></u-icon>
							<u--image :showLoading="true" :src="form.image" width="366rpx"
								height="366rpx"></u--image>
						</view>
						<view class="upload" v-else>
							<view class="unpload-btn" @click="openAuth('CAMERA')">
								<view>点击上传</view>
								<view class="unpload-btn-add">
									<u--image :showLoading="true" src="/static/img/uploadimg_add.png" width="30rpx"
										height="30rpx"></u--image>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="submit">
					<view class="btn" @click="submit">
						提交
					</view>
				</view>
			</view>
		</view>
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
	</view>
</template>

<script>
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	// import permissionPopup from '@/components/permissionPopup/permissionPopup.vue'
	export default {
		data() {
			return {
				form: {
					image: '',
					type: '',
					name: ''
				},
				permissionID:'',
			}
		},
		components: {
			ykAuthpup
		},
		onLoad(e) {
			this.form.type = e.type
			if(this.form.type==1){
				uni.getStorage({
					key:'Alipay',
					success: (res) => {
						this.form = res.data
						if(this.form.image && !this.form.image.includes('http') ){
							this.form.image = this.$store.state.user.baseURL + this.form.image
						}
					},
				})
			}else{
				uni.getStorage({
					key:'WeChat',
					success: (res) => {
						this.form = res.data
						if(this.form.image && !this.form.image.includes('http') ){
							this.form.image = this.$store.state.user.baseURL + this.form.image
						}
					},
				})
			}
		},
		methods: {
			openAuth(permissionID){
				this.permissionID = permissionID;//这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
				setTimeout(()=>{
					this.$refs['authpup'].open();
				},500)
			},
			//用户授权权限后的回调
			changeAuth(){
				//这里是权限通过后执行自己的代码逻辑
				uni.chooseImage({
					count: 1,
					success: (e) => {
						console.log(e)
						uni.uploadFile({
							// url: 'https://app.fjxmlz.com/api/attachment/upload',
							url: 'https://zhaobanma.booster-cloud.cn/admin/addImgDeng',
							filePath: e.tempFilePaths[0],
							name: 'file',
							header: {
								'accessToken': this.$store.state.login.token,
							},
							success: (uploadFileRes) => {
								console.log(uploadFileRes)
								this.form.image  = JSON.parse(uploadFileRes.data).data.url
							}
						});
						
					}
				})
			},
			androidCameraPermiss(index) {
				plus.android.requestPermissions(['android.permission.CAMERA'], (e) => {
					if (e.deniedAlways.length > 0) {
						this.androidAuthCamera = false
						// 弹出提示框解释为何需要权限，引导用户打开设置页面开启  
						uni.showModal({
							title: '提示',
							content: '请打开手机相机相册功能（点击确定后在权限中授权相机相册功能）',
							// showCancel: false, // 不显示取消按钮
							success(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass("android.content.Intent");
									var Settings = plus.android.importClass(
										"android.provider.Settings");
									var Uri = plus.android.importClass("android.net.Uri");
									var mainActivity = plus.android.runtimeMainActivity();
									var intent = new Intent();
									intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
									var uri = Uri.fromParts("package", mainActivity.getPackageName(),
										null);
									intent.setData(uri);
									mainActivity.startActivity(intent);
								}
							}
						});
					} else if (e.deniedPresent.length > 0) { //权限被临时拒绝  
						this.androidAuthCamera = false
						// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限  
						uni.showModal({
							title: '提示',
							content: '请打开手机相机功能（点击确定后在权限中授权相机功能）',
							// showCancel: false, // 不显示取消按钮
							success(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass("android.content.Intent");
									var Settings = plus.android.importClass(
										"android.provider.Settings");
									var Uri = plus.android.importClass("android.net.Uri");
									var mainActivity = plus.android.runtimeMainActivity();
									var intent = new Intent();
									intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
									var uri = Uri.fromParts("package", mainActivity.getPackageName(),
										null);
									intent.setData(uri);
									mainActivity.startActivity(intent);
								}
							}
						});
					} else {
						plus.android.requestPermissions(['android.permission.READ_EXTERNAL_STORAGE'], (e) => {
							if (e.deniedAlways.length > 0) { //权限被永久拒绝  
								// 弹出提示框解释为何需要权限，引导用户打开设置页面开启  
								uni.showModal({
									title: '提示',
									content: '请打开相册存储功能（点击确定后在权限中授权相册存储功能）',
									// showCancel: false, // 不显示取消按钮
									success(res) {
										if (res.confirm) {
											var Intent = plus.android.importClass(
												"android.content.Intent");
											var Settings = plus.android.importClass(
												"android.provider.Settings");
											var Uri = plus.android.importClass("android.net.Uri");
											var mainActivity = plus.android.runtimeMainActivity();
											var intent = new Intent();
											intent.setAction(Settings
												.ACTION_APPLICATION_DETAILS_SETTINGS);
											var uri = Uri.fromParts("package", mainActivity
												.getPackageName(),
												null);
											intent.setData(uri);
											mainActivity.startActivity(intent);
										}
									}
								});
							} else if (e.deniedPresent.length > 0) { //权限被临时拒绝  
								this.androidAuthCamera = false
								// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限  
								uni.showModal({
									title: '提示',
									content: '请打开相册存储功能（点击确定后在权限中授权相册存储功能）',
									// showCancel: false, // 不显示取消按钮
									success(res) {
										if (res.confirm) {
											var Intent = plus.android.importClass(
												"android.content.Intent");
											var Settings = plus.android.importClass(
												"android.provider.Settings");
											var Uri = plus.android.importClass("android.net.Uri");
											var mainActivity = plus.android.runtimeMainActivity();
											var intent = new Intent();
											intent.setAction(Settings
												.ACTION_APPLICATION_DETAILS_SETTINGS);
											var uri = Uri.fromParts("package", mainActivity
												.getPackageName(),
												null);
											intent.setData(uri);
											mainActivity.startActivity(intent);
										}
									}
								});
							} else {
								this.androidAuthCamera = true
								// 选择照片 uni.chooseImage() 
								this.chooseImage(index)
							}
						})
					}
				})
			},

			chooseImage(type) {
				uni.chooseImage({
					count: 1,
					success: (e) => {
						console.log(e)
						uni.uploadFile({
							// url: 'https://app.fjxmlz.com/api/attachment/upload',
							url: 'https://zhaobanma.booster-cloud.cn/admin/addImgWash',
							filePath: e.tempFilePaths[0],
							name: 'file',
							header: {
								'accessToken': this.$store.state.login.token,
							},
							success: (uploadFileRes) => {
								// console.log(uploadFileRes)
								this.form.image = JSON.parse(uploadFileRes.data).data.url
							}
						});
					},
					fail: (e) => {
						if (e.errMsg.includes('No Permission')) this.$store.commit('permissions/goSystemSet', e
							.errCode)
					}
				})
			},
			delImage(){
				this.form.image = ''
			},
			submit() {
				uni.setStorage({
					key: this.form.type == 1 ? 'Alipay' : 'WeChat',
					data: this.form,
					success: () => {
						uni.navigateBack()
					}
				})
				// this.$api.banksave(this.form).then(res =>{

				// })
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background: #f5f5f5;
		min-height: 100vh;
		z-index: -1;
	}

	.box {
		padding: 0 30rpx;
	}

	.withdrawal {
		background: #FFFFFF;
		border-radius: 10rpx;
		min-height: 100vh;
		z-index: -1;
		padding: 0 30rpx;

		.name {
			padding: 48rpx 0;
			border-bottom: 2rpx solid #e7e7e7;
			display: flex;
			justify-content: space-between;

			.title {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 36rpx;
				color: #111111;
				text-align: left;
				font-style: normal;
			}
		}

		.paytitle {
			padding-top: 46rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			color: #111111;
			line-height: 44rpx;
			text-align: left;
			font-style: normal;
		}

		.uploadimg {
			display: flex;
			justify-content: center;
			padding-top: 125rpx;

			.uploadimg-bg {
				box-sizing: border-box;
				width: 430rpx;
				height: 430rpx;
				background: url('@/static/img/uploadimg_bg.png');
				background-size: 100% 100%;
				padding: 32rpx;

				.upload {
					width: 100%;
					height: 100%;
					border-radius: 40rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					position: relative;
					.unpload-btn {
						width: 230rpx;
						height: 64rpx;
						background: linear-gradient(270deg, #EA6F29 0%, #FAAA7D 100%);
						border-radius: 16rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 34rpx;
						color: #FFFFFF;
						line-height: 64rpx;
						text-align: center;
						font-style: normal;
						display: flex;
						justify-content: center;
						align-items: center;

						.unpload-btn-add {
							padding-left: 14rpx;
						}
					}
					.del-btn{
						position: absolute;
						z-index: 99;
						right: -20rpx;
						top: -20rpx;
						width: 60rpx;
						height: 60rpx;
						border-radius: 50%;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}
			}
		}
	}

	.submit {
		padding-top: 120rpx;

		.btn {
			width: 630rpx;
			height: 84rpx;
			background: linear-gradient(270deg, #EA6F29 0%, #FAAA7D 100%);
			border-radius: 16rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 34rpx;
			color: #FFFFFF;
			line-height: 84rpx;
			text-align: center;
			font-style: normal;
		}
	}
</style>